﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GamePlayerInfo : MonoBehaviour
{
    public Image m_imgDaojishi;

    public Image m_imgHead;

    public Text m_txtName;

    public GameObject[] qizi;
    public GameObject qizi_quick;

    public Image m_imgShaizi;

    public GameObject m_light;
    public Button m_btnShaizi;
    
    
    
    private float daojishi = 0;
    private float daojishiEnd = 10;
    public bool m_StartGame = false;

    private bool m_isSuijishaizi = false;
    //是不是可以摇色子，如果可以就直接摇，2秒结束摇动
    private float shujushaiziEnd = 3;

    public int m_shaizhirandom = 0;
    private bool m_isSelf = false;
    
    
    // Start is called before the first frame update
    void Start()
    {
        m_btnShaizi.onClick.AddListener(() =>
        {
            if (m_isSelf)
            {
                suijijiange = 0;
                m_isSuijishaizi = true;
                m_shaizhirandom = Random.Range(0, 4);
            }
        });
    }

    private float suijijiange =0;

    private float suijijiageEnd = 0.15f;

    private float shaizidaojishi = 0;
    // Update is called once per frame
    void Update()
    {
        if (m_StartGame)
        {
            daojishi += Time.deltaTime;
            suijijiange += Time.deltaTime;
            m_imgDaojishi.fillAmount = (daojishiEnd - daojishi) / daojishiEnd;
            
            if (daojishi >= daojishiEnd)
            {
                EndOPT();
            }
            
            if (m_isSuijishaizi)
            {
                shaizidaojishi += Time.deltaTime;
                if (suijijiange >= suijijiageEnd)
                {
                    
                    suijijiange = suijijiange - suijijiageEnd;
                    var shaizilist = LocalData.Instance.OnGetShaizi();
                    Debug.LogError(shaizidaojishi);
                    if (shaizidaojishi < shujushaiziEnd && daojishi <= 7)
                    {
                        m_imgShaizi.sprite = shaizilist[Random.Range(0, shaizilist.Count-1)];
                    }
                    else
                    {
                        m_imgShaizi.sprite = shaizilist[m_shaizhirandom]; 
                    }
                }
            }
        }
    }

    public void OnInit()
    {
        shaizidaojishi = 0;
        daojishi = 0;
        m_StartGame = false;
        m_isSuijishaizi = false;
        m_imgDaojishi.fillAmount = 0;
        m_shaizhirandom = 0;
        m_imgShaizi.gameObject.SetActive(false);
        m_light.gameObject.SetActive(false);
        
        if (LocalData.Instance.EnterGameType == 1)
        {
            for (int i = 0; i < qizi.Length; i++)
            {
                qizi[i].gameObject.SetActive(false);
            }
            qizi_quick.gameObject.SetActive(true);
        }
        else
        {
            for (int i = 0; i < qizi.Length; i++)
            {
                qizi[i].gameObject.SetActive(true);
            }
            qizi_quick.gameObject.SetActive(false);
        }
    }

    public void StartOPT(bool isself)
    {
        shaizidaojishi = 0;
        daojishi = 0;
        m_imgDaojishi.fillAmount = 1;
        m_light.gameObject.SetActive(true);
        m_imgShaizi.gameObject.SetActive(true);
        var shaizilist = LocalData.Instance.OnGetShaizi();
        m_imgShaizi.sprite = shaizilist[shaizilist.Count-1];
        if (!isself)
        {
            m_isSuijishaizi = true;//Random.Range(0, 10) >= 9;
            m_shaizhirandom = Random.Range(0, 4);
        }

        m_isSelf = isself;
        
        m_StartGame = true;
    }
    public void EndOPT()
    {
        daojishi = 0;
        m_StartGame = false;
        m_isSuijishaizi = false;
        m_imgDaojishi.fillAmount = 0;
        shaizidaojishi = 0;
        EventCenter.BroadCast(EventType.Game_OPT_END);
    }

    public void OnDOMovePos()
    {
        m_imgShaizi.gameObject.SetActive(false);
        m_light.gameObject.SetActive(false);
        EventCenter.BroadCast(EventType.Game_Move_END);
    }
    
}
